# Replication Archive for: 
# Coppock, Alexander and Donald P. Green. 2020. 
# "Do Belief Systems Exhibit Dynamic Constraint?" 
# The Journal of Politics, Forthcoming.

rm(list = ls())

library(tidyverse)
library(xtable)
library(janitor)

original <- read_rds("data/clean/original_trump_white_clean.rds")
replication <-
  read_rds("data/clean/replication_trump_white_clean.rds")

study_3_stacked <-
  bind_rows(
    Original = dplyr::select(original, age_5, educ_5, race_4, pid_7 = pid_7n, female),
    Replication = dplyr::select(replication, age_5, educ_5, race_4, pid_7, female),
    .id = "sample"
  ) %>%
  mutate(sample = factor(sample, levels = c("Original", "Replication")))


original %>% tabyl(age_5)

age_tab <-
study_3_stacked %>%
  tabyl(age_5, sample) %>%
  adorn_percentages(denominator = "col") %>%
  adorn_pct_formatting()

educ_tab <-
study_3_stacked %>%
  tabyl(educ_5, sample) %>%
  adorn_percentages(denominator = "col") %>%
  adorn_pct_formatting()

race_tab <-
  study_3_stacked %>%
  tabyl(race_4, sample) %>%
  adorn_percentages(denominator = "col") %>%
  adorn_pct_formatting()

pid_tab <-
  study_3_stacked %>%
  mutate(pid_7 = factor(pid_7, levels = 1:7,
                        labels = c("Strong Democrat",
                                   "Not very strong Democrat",
                                   "Lean Democrat",
                                   "Independent",
                                   "Lean Republican",
                                   "Not very strong Republican",
                                   "Strong Republican"))) %>%
  tabyl(pid_7, sample) %>%
  adorn_percentages(denominator = "col") %>%
  adorn_pct_formatting()


female_tab <-
  study_3_stacked %>%
  mutate(female = factor(
    female,
    levels = c(0, 1),
    labels = c("Male", "Female")
  )) %>%
  tabyl(female, sample) %>%
  adorn_percentages(denominator = "col") %>%
  adorn_pct_formatting()


n_tab <-
  study_3_stacked %>%
  mutate(N = "N") %>%
  tabyl(N, sample)


xtable(age_tab, digits = 1) %>% 
  print.xtable(
  file = "../Drafts/tables/study_3_age.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)

xtable(educ_tab, digits = 1) %>% 
  print.xtable(
  file = "../Drafts/tables/study_3_educ.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)

xtable(race_tab, digits = 1) %>% 
  print.xtable(
  file = "../Drafts/tables/study_3_race.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)

xtable(pid_tab, digits = 1) %>% 
  print.xtable(
  file = "../Drafts/tables/study_3_pid.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)

xtable(female_tab, digits = 1) %>% 
  print.xtable(
  file = "../Drafts/tables/study_3_female.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)

xtable(n_tab, digits = 0) %>% 
  print.xtable(
  file = "../Drafts/tables/study_3_n.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)
